import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class LTTToggleButton extends StatefulWidget {
  final bool initialValue;
  final ValueChanged<bool>? onChanged;
  final String yesText;
  final String noText;
  final Color activeColor;
  final Color inactiveColor;
  final Color activeTextColor;
  final Color inactiveTextColor;

  const LTTToggleButton({
    super.key,
    this.initialValue = false,
    this.onChanged,
    this.yesText = '是',
    this.noText = '否',
    this.activeColor = const Color(0xFF2528CB),
    this.inactiveColor = const Color(0xFFFFFFFF),
    this.activeTextColor = Colors.white,
    this.inactiveTextColor = const Color(0xFF999999),
  });

  @override
  State<LTTToggleButton> createState() => _LTTToggleButtonState();
}

class _LTTToggleButtonState extends State<LTTToggleButton> {
  late bool _isYes;

  @override
  void initState() {
    super.initState();
    _isYes = widget.initialValue;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        border: Border.all(width: 1.w, color: const Color(0xFF2952E5)),
        borderRadius: BorderRadius.circular(4.r),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          _buildButton(
              text: widget.yesText, isActive: _isYes, onTap: () => _toggle(true), isLeft: true),
          _buildButton(
              text: widget.noText, isActive: !_isYes, onTap: () => _toggle(false), isLeft: false),
        ],
      ),
    );
  }

  Widget _buildButton({
    required String text,
    required bool isActive,
    required VoidCallback onTap,
    required bool isLeft,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: AnimatedContainer(
        duration: const Duration(milliseconds: 200),
        curve: Curves.easeInOut,
        padding: EdgeInsets.symmetric(horizontal: 13.w, vertical: 6.h),
        decoration: BoxDecoration(
          color: isActive ? widget.activeColor : widget.inactiveColor,
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(isLeft ? 3.r : 0),
            bottomLeft: Radius.circular(isLeft ? 3.r : 0),
            topRight: Radius.circular(isLeft ? 0 : 3.r),
            bottomRight: Radius.circular(isLeft ? 0 : 3.r),
          ),
          boxShadow: isActive
              ? [
                  BoxShadow(
                    color: widget.activeColor.withOpacity(0.3),
                    blurRadius: 8,
                    spreadRadius: 1,
                  )
                ]
              : null,
        ),
        child: Text(
          text,
          style: TextStyle(
            color: isActive ? widget.activeTextColor : widget.inactiveTextColor,
            fontSize: 15.sp,
          ),
        ),
      ),
    );
  }

  void _toggle(bool value) {
    setState(() {
      _isYes = value;
    });
    widget.onChanged?.call(value);
  }
}
